package cn.iocoder.yudao.module.nginxui.controller.admin.httplocation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.nginxui.controller.admin.httplocation.vo.*;
import cn.iocoder.yudao.module.nginxui.dal.dataobject.httplocation.HttpLocationDO;
import cn.iocoder.yudao.module.nginxui.service.httplocation.HttpLocationService;

@Tag(name = "管理后台 - Nginx Server下的Location块配置")
@RestController
@RequestMapping("/nginxui/http-location")
@Validated
public class HttpLocationController {

    @Resource
    private HttpLocationService httpLocationService;

    @PostMapping("/create")
    @Operation(summary = "创建Nginx Server下的Location块配置")
    @PreAuthorize("@ss.hasPermission('nginxui:http-location:create')")
    public CommonResult<Long> createHttpLocation(@Valid @RequestBody HttpLocationSaveReqVO createReqVO) {
        return success(httpLocationService.createHttpLocation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新Nginx Server下的Location块配置")
    @PreAuthorize("@ss.hasPermission('nginxui:http-location:update')")
    public CommonResult<Boolean> updateHttpLocation(@Valid @RequestBody HttpLocationSaveReqVO updateReqVO) {
        httpLocationService.updateHttpLocation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除Nginx Server下的Location块配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('nginxui:http-location:delete')")
    public CommonResult<Boolean> deleteHttpLocation(@RequestParam("id") Long id) {
        httpLocationService.deleteHttpLocation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除Nginx Server下的Location块配置")
                @PreAuthorize("@ss.hasPermission('nginxui:http-location:delete')")
    public CommonResult<Boolean> deleteHttpLocationList(@RequestParam("ids") List<Long> ids) {
        httpLocationService.deleteHttpLocationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得Nginx Server下的Location块配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('nginxui:http-location:query')")
    public CommonResult<HttpLocationRespVO> getHttpLocation(@RequestParam("id") Long id) {
        HttpLocationDO httpLocation = httpLocationService.getHttpLocation(id);
        return success(BeanUtils.toBean(httpLocation, HttpLocationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得Nginx Server下的Location块配置分页")
    @PreAuthorize("@ss.hasPermission('nginxui:http-location:query')")
    public CommonResult<PageResult<HttpLocationRespVO>> getHttpLocationPage(@Valid HttpLocationPageReqVO pageReqVO) {
        PageResult<HttpLocationDO> pageResult = httpLocationService.getHttpLocationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HttpLocationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出Nginx Server下的Location块配置 Excel")
    @PreAuthorize("@ss.hasPermission('nginxui:http-location:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportHttpLocationExcel(@Valid HttpLocationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HttpLocationDO> list = httpLocationService.getHttpLocationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "Nginx Server下的Location块配置.xls", "数据", HttpLocationRespVO.class,
                        BeanUtils.toBean(list, HttpLocationRespVO.class));
    }

}